import {getAssistant} from '@/api/commodityfiles'
import { throttle } from 'lodash'
import AMap from 'AMap'

export function getWarehouseLabel(item) {
  return `<div class='info'>${item.name}
            <div>${item.kitchenName ? `中央厨房：${item.kitchenName}` : ''}</div>
            <div>${item.productionName ? `生产站：${item.productionName}` : ''}</div>
            <div>${item.transferName ? `中转仓：${item.transferName}` : ''}</div>
          </div>`
}

// 关键字搜索地址
export const inputTip = (options) => {
  const {keyword, city} = options
  return new Promise(resolve => {
    AMap.plugin('AMap.Autocomplete', () => {
      var autoOptions = {
        city: city || ''
      }
      const autoComplete = new AMap.Autocomplete(autoOptions);
      autoComplete.search(keyword, (status, result) => {
        if (result.tips) {
          // console.log(result)
          resolve(result.tips)
        } else {
          getAssistant(options).then(res => {
            const list = (res.data.tips || []).map(item => {
              const locations = item.location.split(',')
              const location = {
                lng: locations[0],
                lat: locations[1],
              }
              return {
                ...item,
                location,
              }
            })
            resolve(list)
          })
        }
      })
    })
  })
}
export const inputTipThrottle = throttle(inputTip, 1000)